<?php

namespace app\admin\controller\product;

//use app\admin\controller\shopro\Common;
//use app\admin\model\shopro\goods\Goods as GoodsModel;
//use app\admin\model\shopro\goods\Sku as SkuModel;
//use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
//use app\admin\model\shopro\goods\StockWarning as StockWarningModel;
//use app\admin\model\shopro\activity\Activity as ActivityModel;
//use app\admin\controller\shopro\traits\SkuPrice as SkuPriceTrait;
//use addons\shopro\traits\StockWarning as StockWarningTrait;
//use addons\shopro\service\goods\GoodsService;
use app\admin\model\product\GoodsModel;
use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\response\Json;

/**
 * 商品管理
 */
class Goods extends Backend
{

//    use SkuPriceTrait, StockWarningTrait;

//    protected $noNeedRight = ['getType', 'select', 'activitySelect'];

    /**
     * 商品模型对象
     * @var \app\admin\model\shopro\goods\Goods
     */
    protected $model = null;
    protected $activityModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new GoodsModel();
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

//        $goodsTableName = $this->model->getQuery()->getTable();
        $goods = $this->model->sheepFilter();
//        $goods = $goods->join(['product_category' => 'pc'], $goodsTableName . '.id = sp.sku_goods_id', 'left')
        $where= $this->buildwheres();
        $goods = $goods
            ->field("*")
            ->where($where)
            ->where(['deletetime'=>0])
            ->paginate($this->request->param('list_rows', 10));
//            ->paginate($this->request->param('list_rows', 10));
        $goods->each(function ($item){
            //商品状态:up=上架,hidden=隐藏,down=下架
            if($item->status == 'up'){
                $item->status_texts= '上架';
            }elseif ($item->status == 'hidden'){
                $item->status_texts= '隐藏';
            }else{
                $item->status_texts= '下架';
            }
            return $item;
        });
        $this->success('获取成功', null, $goods);
    }


    // 获取数据类型
    public function getType()
    {
//        $activityTypes = $this->activityModel->typeList();
        $statusList = $this->model->statusList();

        $result = [
//            'activity_type' => $activityTypes,
            'status' => $statusList
        ];

        $data = [];
        foreach ($result as $key => $list) {
            $data[$key][] = ['name' => '全部', 'type' => 'all'];

            foreach ($list as $k => $v) {
                $data[$key][] = [
                    'name' => $v,
                    'type' => $k
                ];
            }
        }

        $this->success('获取成功', null, $data);
    }




    /**
     * 添加
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $params = $this->request->only([
             'title',  'category_ids', 'image', 'images',
             'price', 'status', 'weigh','virtually_nums','goods_desc','is_hot'
        ]);         // likes, views, sales,
        if(isset($params['category_ids']) && !empty($params['category_ids'])){
            $idArr= explode(',',$params['category_ids']);
            if(count($idArr) >1){
                $this->error("分类ID只能选择一个");
            }
        }
        $params['content'] = $this->request->param('content', '', null);      // content 不经过全局过滤
//        $this->svalidate($params, ".add");

        $data = Db::transaction(function () use ($params) {
            $this->model->save($params);
        });
        $this->success('保存成功', null, $data);
    }




    /**
     * 详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $goods = $this->model->where('id', $id)->find();
        if (!$goods) {
            $this->error(__('No Results were found'));
        }
        $goods->category_ids_arr = $goods->category_ids_arr;

        $content = $goods['content'];
        $goods = $goods->toArray();
        $goods['content'] = $content;
        $this->success('保存成功', null, $goods);
    }


    /**
     * 编辑(支持批量)
     */
    public function edit($id = null)
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch('add');
        }
        $params = $this->request->only([
            'title',  'category_ids', 'image', 'images',
            'price', 'status', 'weigh','virtually_nums','goods_desc','is_hot','is_technology'
        ]);
        if(isset($params['category_ids']) && !empty($params['category_ids'])){
            $idArr= explode(',',$params['category_ids']);
            if(count($idArr) >1){
                $this->error("分类ID只能选择一个");
            }
        }

        if(count($params) > 1){
            $params['content'] = $this->request->param('content', '', null);         // content 不经过全局过滤
            $params['category_ids'] = $this->request->param('category_ids', '');        // 分类不判空
        }
        $id = explode(',', $id);
        $items = $this->model->whereIn('id', $id)->select();
        Db::transaction(function () use ($items, $params) {
            foreach ($items as $goods) {
                $params['images'] = json_encode($params['images']);
                $goods->where(['id'=>$goods['id']])->update($params);
            }
        });

        $this->success('更新成功', null);
    }


//    public function addStock($id)
//    {
//        if (!$this->request->isAjax()) {
//            return $this->view->fetch();
//        }
//
//        $goods = $this->model->where('id', $id)->find();
//        if (!$goods) {
//            $this->error(__('No Results were found'));
//        }
//        if ($goods->is_sku) {
//            // 多规格
//            $skuPrices = $this->request->post('sku_prices/a', []);
//            foreach ($skuPrices as $skuPrice) {
//                if (isset($skuPrice['add_stock']) && $skuPrice['add_stock'] != 0 && $skuPrice['id']) {
//                    $skuPriceModel = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find($skuPrice['id']);
//                    if ($skuPriceModel) {
//                        Db::transaction(function () use ($skuPriceModel, $skuPrice) {
//                            $this->addStockToSkuPrice($skuPriceModel, $skuPrice['add_stock'], 'goods');
//                        });
//                    }
//                }
//            }
//        } else {
//            $add_stock = $this->request->param('add_stock', 0);
//            $skuPriceModel = SkuPriceModel::where('goods_id', $id)->order('id', 'asc')->find();
//
//            if ($skuPriceModel) {
//                Db::transaction(function () use ($skuPriceModel, $add_stock) {
//                    $this->addStockToSkuPrice($skuPriceModel, $add_stock, 'goods');
//                });
//            }
//        }
//
//        $this->success('补货成功');
//    }



    public function select()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $type = $this->request->param('type', 'page');
        $goodsTableName = $this->model->getQuery()->getTable();

        $goods = $this->model->sheepFilter()->with(['max_sku_price']);

        // 聚合库存 (包含下架的规格)
        $skuSql = SkuPriceModel::field('sum(stock) as stock, goods_id as sku_goods_id')->group('goods_id')->buildSql();
        $goods = $goods->join([$skuSql => 'sp'], $goodsTableName . '.id = sp.sku_goods_id', 'left')
            ->field("$goodsTableName.*, sp.stock");       // ,score.*

        if ($type == 'select') {
            // 普通结果
            $goods = collection($goods->select());
        } else {
            // 分页结果
            $goods = $goods->paginate($this->request->param('list_rows', 10));
        }

        $goods = $goods->each(function ($goods) {
            // 获取活动信息
            $goods->activities = $goods->activities;
            $goods->promos = $goods->promos;

            $data_type = $this->request->param('data_type', '');       // 特殊 type 需要处理的数据
            if ($data_type == 'score_shop') {
                $goods->is_score_shop = $goods->is_score_shop;
            }
        });

        $this->success('获取成功', null, $goods);
    }



    /**
     * 获取指定活动相关商品
     *
     * @param Request $request
     * @return void
     */
//    public function activitySelect()
//    {
//        $activity_id = $this->request->param('activity_id');
//        $need_buyers = $this->request->param('need_buyers', 0);      // 需要查询哪些人在参与活动
//        $activity = $this->activityModel->where('id', $activity_id)->find();
//        if (!$activity) {
//            $this->error(__('No Results were found'));
//        }
//        $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
//
//        // 存一下，获取器获取指定活动的时候会用到
//        foreach ($goodsIds as $id) {
//            session('goods-activity_id:' . $id, $activity_id);
//        }
//        $service = new GoodsService(function ($goods) use ($need_buyers) {
//            if ($need_buyers) {
//                $goods->buyers = $goods->buyers;
//            }
//            $goods->activity = $goods->activity;
//            return $goods;
//        });
//
//        $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->select();
//        $goods = collection($goods)->toArray();     // 可以将里面的单个 model也转为数组
//        foreach ($goods as &$gd) {
//            unset($gd['new_sku_prices'], $gd['activity']);
//        }
//        $this->success('获取成功', null, $goods);
//    }




    /**
     * 删除(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $list = $this->model->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($list) {
            foreach ($list as $item) {
                GoodsModel::where(['id'=>$item['id']])->update(['deletetime'=>time()]);
            }
        });

        $this->success('删除成功');
    }


    public function recyclebin()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $goods = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));
        $this->success('获取成功', null, $goods);
    }


    /**
     * 还原(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function restore($id = null)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($items) {
            $count = 0;
            foreach ($items as $item) {
                $count += $item->restore();
            }

            return $count;
        });

        if ($result) {
            $this->success('还原成功', null, $result);
        } else {
            $this->error(__('No rows were updated'));
        }
    }


    /**
     * 销毁(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function destroy($id = null)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        if ($id !== 'all') {
            $items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
        } else {
            $items = $this->model->onlyTrashed()->select();
        }
        $result = Db::transaction(function () use ($items) {
            $count = 0;
            foreach ($items as $goods) {
                // 删除商品相关的规格，规格记录
                SkuModel::where('goods_id', $goods->id)->delete();
                SkuPriceModel::where('goods_id', $goods->id)->delete();

                // 删除商品
                $count += $goods->delete(true);
            }
            return $count;
        });

        if ($result) {
            $this->success('销毁成功', null, $result);
        }
        $this->error('销毁失败');
    }

    /**
     * 认证选择
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
//    public function selects()
//    {
//        //设置过滤方法
//        $this->request->filter(['strip_tags', 'trim']);
//        if (false === $this->request->isAjax()) {
//            return $this->view->fetch();
//        }
//        //如果发送的来源是 Selectpage，则转发到 Selectpage
//        if ($this->request->request('keyField')) {
//            return $this->selectpage();
//        }
//        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
//        $list = $this->model
//            ->where($where)
//            ->order($sort, $order)
//            ->paginate($limit);
//        $result = ['total' => $list->total(), 'rows' => $list->items()];
//        return json($result);
//    }


}
